<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="sku" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入sku"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺id" prop="shopId">
        <el-input
          v-model="queryParams.shopId"
          placeholder="请输入店铺id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          :icon="!isOpen ? 'el-icon-arrow-down' : 'el-icon-arrow-up'"
          size="mini"
          @click="shouqi"
          >展开/收起</el-button
        >
      </el-form-item>

      <div v-if="isOpen">
        <el-form-item label="销售金额" prop="salesAmount">
          <el-input
            v-model="queryParams.salesAmount"
            placeholder="请输入销售金额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="环比销售金额" prop="chainSalesAmount">
          <el-input
            v-model="queryParams.chainSalesAmount"
            placeholder="请输入环比销售金额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="同比销售金额" prop="yoySalesAmount">
          <el-input
            v-model="queryParams.yoySalesAmount"
            placeholder="请输入同比销售金额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售数量" prop="salesQuantity">
          <el-input
            v-model="queryParams.salesQuantity"
            placeholder="请输入销售数量"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="净销售数量" prop="netSalesQuantity">
          <el-input
            v-model="queryParams.netSalesQuantity"
            placeholder="请输入净销售数量"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="净销售金额" prop="netSalesAmount">
          <el-input
            v-model="queryParams.netSalesAmount"
            placeholder="请输入净销售金额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="商品总成本" prop="salesCost">
          <el-input
            v-model="queryParams.salesCost"
            placeholder="请输入商品总成本"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="日常价销售总额" prop="salePriceAmount">
          <el-input
            v-model="queryParams.salePriceAmount"
            placeholder="请输入日常价销售总额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="环比日常价销售总额" prop="chainSalePriceAmount">
          <el-input
            v-model="queryParams.chainSalePriceAmount"
            placeholder="请输入环比日常价销售总额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="同比日常价销售总额" prop="yoySalePriceAmount">
          <el-input
            v-model="queryParams.yoySalePriceAmount"
            placeholder="请输入同比日常价销售总额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </div>
      <!-- <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item> -->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sale:BiProTypeMiddleDwd:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sale:BiProTypeMiddleDwd:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sale:BiProTypeMiddleDwd:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sale:BiProTypeMiddleDwd:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" @click="handlesynData">同步</el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="BiProTypeMiddleDwdList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="sku" align="center" prop="sku" />
      <el-table-column label="店铺id" align="center" prop="shopId" />
      <el-table-column label="店铺名称" align="center" prop="shopName" />
      <el-table-column label="日期类型" align="center" prop="dateType" />
      <el-table-column label="销售金额" align="center" prop="salesAmount" />
      <el-table-column
        label="环比销售金额"
        align="center"
        prop="chainSalesAmount"
      />
      <el-table-column
        label="同比销售金额"
        align="center"
        prop="yoySalesAmount"
      />
      <el-table-column label="销售数量" align="center" prop="salesQuantity" />
      <el-table-column
        label="净销售数量"
        align="center"
        prop="netSalesQuantity"
      />
      <el-table-column
        label="净销售金额"
        align="center"
        prop="netSalesAmount"
      />
      <el-table-column label="商品总成本" align="center" prop="salesCost" />
      <el-table-column
        label="日常价销售总额"
        align="center"
        prop="salePriceAmount"
      />
      <el-table-column
        label="环比日常价销售总额"
        align="center"
        prop="chainSalePriceAmount"
      />
      <el-table-column
        label="同比日常价销售总额"
        align="center"
        prop="yoySalePriceAmount"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sale:BiProTypeMiddleDwd:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sale:BiProTypeMiddleDwd:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品品类分析缓存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="sku" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入sku" />
        </el-form-item>
        <el-form-item label="店铺id" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入店铺id" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="销售金额" prop="salesAmount">
          <el-input v-model="form.salesAmount" placeholder="请输入销售金额" />
        </el-form-item>
        <el-form-item label="环比销售金额" prop="chainSalesAmount">
          <el-input
            v-model="form.chainSalesAmount"
            placeholder="请输入环比销售金额"
          />
        </el-form-item>
        <el-form-item label="同比销售金额" prop="yoySalesAmount">
          <el-input
            v-model="form.yoySalesAmount"
            placeholder="请输入同比销售金额"
          />
        </el-form-item>
        <el-form-item label="销售数量" prop="salesQuantity">
          <el-input v-model="form.salesQuantity" placeholder="请输入销售数量" />
        </el-form-item>
        <el-form-item label="净销售数量" prop="netSalesQuantity">
          <el-input
            v-model="form.netSalesQuantity"
            placeholder="请输入净销售数量"
          />
        </el-form-item>
        <el-form-item label="净销售金额" prop="netSalesAmount">
          <el-input
            v-model="form.netSalesAmount"
            placeholder="请输入净销售金额"
          />
        </el-form-item>
        <el-form-item label="商品总成本" prop="salesCost">
          <el-input v-model="form.salesCost" placeholder="请输入商品总成本" />
        </el-form-item>
        <el-form-item label="日常价销售总额" prop="salePriceAmount">
          <el-input
            v-model="form.salePriceAmount"
            placeholder="请输入日常价销售总额"
          />
        </el-form-item>
        <el-form-item label="环比日常价销售总额" prop="chainSalePriceAmount">
          <el-input
            v-model="form.chainSalePriceAmount"
            placeholder="请输入环比日常价销售总额"
          />
        </el-form-item>
        <el-form-item label="同比日常价销售总额" prop="yoySalePriceAmount">
          <el-input
            v-model="form.yoySalePriceAmount"
            placeholder="请输入同比日常价销售总额"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiProTypeMiddleDwd,
  getBiProTypeMiddleDwd,
  delBiProTypeMiddleDwd,
  addBiProTypeMiddleDwd,
  updateBiProTypeMiddleDwd,
  synData,
} from "@/api/sale/BiProTypeMiddleDwd";

export default {
  name: "BiProTypeMiddleDwd",
  data() {
    return {
      isOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品品类分析缓存表格数据
      BiProTypeMiddleDwdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sku: null,
        shopId: null,
        shopName: null,
        dateType: null,
        salesAmount: null,
        chainSalesAmount: null,
        yoySalesAmount: null,
        salesQuantity: null,
        netSalesQuantity: null,
        netSalesAmount: null,
        salesCost: null,
        salePriceAmount: null,
        chainSalePriceAmount: null,
        yoySalePriceAmount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在同步数据，请稍候！");
    },
    handlesynData() {
      this.openLoading();
      synData().then((response) => {
        this.$modal.closeLoading();
        if (response.code == 200) {
          this.$modal.msgSuccess("同步成功");
          this.getList();
        }
      });
    },
    //搜索栏展示与收起
    shouqi() {
      // console.log(1);
      this.isOpen = !this.isOpen;
    },
    /** 查询商品品类分析缓存列表 */
    getList() {
      this.loading = true;
      listBiProTypeMiddleDwd(this.queryParams).then((response) => {
        this.BiProTypeMiddleDwdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sku: null,
        shopId: null,
        shopName: null,
        dateType: null,
        salesAmount: null,
        chainSalesAmount: null,
        yoySalesAmount: null,
        salesQuantity: null,
        netSalesQuantity: null,
        netSalesAmount: null,
        salesCost: null,
        salePriceAmount: null,
        chainSalePriceAmount: null,
        yoySalePriceAmount: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品品类分析缓存";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiProTypeMiddleDwd(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品品类分析缓存";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiProTypeMiddleDwd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiProTypeMiddleDwd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商品品类分析缓存编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiProTypeMiddleDwd(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sale/BiProTypeMiddleDwd/export",
        {
          ...this.queryParams,
        },
        `BiProTypeMiddleDwd_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
